package com.zhang.modules.file.controller;

import com.zhang.common.jpa.entity.DTO;
import com.zhang.common.web.respWrapper.Res;
import com.zhang.modules.api.annotation.ApiPermission;
import com.zhang.modules.file.entity.CosFileInfo;
import com.zhang.modules.file.entity.cmd.CosFilePageCmd;
import com.zhang.modules.file.entity.cmd.FileNamePageCmd;
import com.zhang.modules.file.entity.dto.CosFileInfoDto;
import com.zhang.modules.file.service.ICosFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Api(tags = "文件管理")
@RequestMapping("/api/file")
@ApiPermission("sys:file:*")
public class FileController {

    private ICosFileService service;

    public FileController(ICosFileService service) {
        this.service = service;
    }

    @ApiOperation("添加文件cos信息")
    @ApiPermission("sys:file:cosFilePage")
    @GetMapping("/cosFilePage")
    public Res cosFilePage(CosFilePageCmd pageCmd, HttpServletRequest request){
        Page<DTO> result = service.page(pageCmd, request.getParameterMap());
        return Res.ok(result);
    }

    @ApiOperation("添加文件cos信息")
    @ApiPermission("sys:file:addCosInfo")
    @PostMapping("/addCosFileInfo")
    public Res addCosFileInfo(@RequestBody CosFileInfo cosFileInfo){
        service.save(cosFileInfo);
        return Res.ok();
    }


    @ApiOperation("查询文件是否已经上传")
    @ApiPermission("sys:file:isExists")
    @PostMapping("/isExists")
    public Res isExists(@RequestBody CosFileInfo cosFileInfo){
        Boolean exists = service.isExists(cosFileInfo);
        return Res.ok(exists);
    }

    @ApiOperation("查询objectKey")
    @ApiPermission("sys:file:detail")
    @GetMapping("/detail")
    public Res detail(@NotBlank(message = "id不能为空") String id){
        String objectKey = service.get(id).getObjectKey();
        return Res.ok("",objectKey);
    }

    @ApiOperation("模糊查询文件名称")
    @ApiPermission("sys:file:pageByName")
    @GetMapping("/pageByName")
    public Res pageByFileNameLike(@Valid FileNamePageCmd cmd){
        Page<CosFileInfoDto> results = service.pageByFileNameLike(cmd);
        return Res.ok(results);
    }
}
